package cn.wx;

import cn.wx.common.properties.WxProperties;
import cn.wx.properties.WxMpProperties;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.util.CollectionUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.wx.properties.WxMpProperties.PREFIX;

/**
 * 默认配置工厂实现
 *
 * @author: sunshaoping
 * @date: Create by in 10:28 上午 2020/5/26
 */
@AllArgsConstructor
public class DefaultWxMpConfigStorageFactory implements WxMpConfigStorageFactory {

    private final WxMpProperties properties;

    @Override
    public Map<String, WxMpConfigStorage> create() {
        properties.verification(PREFIX);

        Map<String, WxMpConfigStorage> configStorageHashMap = Maps.newHashMap();

        //默认配置处理
        WxMpConfigStorage configStorage = createWxMpConfigStorage(properties);
        configStorageHashMap.put(WxMpProperties.DEFAULT, createWxMpConfigStorage(properties));
        configStorageHashMap.put(properties.getName(), configStorage);

        List<WxProperties> multiple = properties.getMultiple();

        if (CollectionUtils.isEmpty(multiple)) {
            return configStorageHashMap;
        }

        //多公众号处理
        Set<String> set = new HashSet<>(multiple.size() + 1);
        set.add(properties.getName());
        for (int i = 0; i < multiple.size(); i++) {
            WxProperties mpProperties = multiple.get(i);
            String prefix = PREFIX + ".multiple[" + i + "]";
            mpProperties.verification(prefix);
            String name = mpProperties.getName();
            if (set.contains(name)) {
                throw new IllegalStateException(prefix + ".name=" + name + ",已存在");
            }
            set.add(name);
            configStorageHashMap.put(name, createWxMpConfigStorage(mpProperties));
        }

        return configStorageHashMap;
    }

    protected WxMpConfigStorage createWxMpConfigStorage(WxProperties properties) {
        WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
        configStorage.setAppId(properties.getAppId());
        configStorage.setSecret(properties.getSecret());
        configStorage.setToken(properties.getToken());
        configStorage.setAesKey(properties.getAesKey());

        ApacheHttpClientBuilder httpClientBuilder = createApacheHttpClientBuilder();

        configStorage.setApacheHttpClientBuilder(httpClientBuilder);
        return configStorage;
    }

    protected ApacheHttpClientBuilder createApacheHttpClientBuilder() {
        DefaultApacheHttpClientBuilder httpClientBuilder = DefaultApacheHttpClientBuilder.get();
        httpClientBuilder.setConnectionRequestTimeout(-1);//默认设置情况下多线程并发推送模板消息会有部分线程获取不到连接导致发送失败
        httpClientBuilder.setMaxConnPerHost(50);
        return httpClientBuilder;
    }
}
